package com.gl.battleship.domain;

import java.util.ArrayList;
import java.util.List;

public class Player {

	private User user;
	
	private List<Ship> fleet = new ArrayList<Ship>();

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Ship> getFleet() {
		return fleet;
	}

	public void setFleet(List<Ship> fleet) {
		this.fleet = fleet;
	}
	
	public String shoot(int x, int y){
		int size = 0;
		for(Ship ship:getFleet()){
			for(Position position: ship.getPositions()){
				if(position.getX()==x && position.getY() == y){
					position.setStatePosition(StatePosition.HIT);
					ship.resfreshShip();
					
					if(ship.getStateShip() == ShipState.HIT)
					{						
						return Constants.HIT;
					}
					
					if(ship.getStateShip() == ShipState.SUNK)
					{
						for(Ship ship2: getFleet()){
							if(ship2.getStateShip() == ShipState.SUNK){
								size++;
							}
						}
						if(getFleet().size() == size){
							return Constants.HIT_SUNK_WIN;
						}else{
							return Constants.HIT_SUNK;
						}
					}
					
				}
			}
		}
		return Constants.WATER;
	}
}
